package com.bizdroid.unutturma.activity;

import java.util.UUID;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.bizdroid.unutturma.R;
import com.bizdroid.unutturma.utility.Utility;

public class NewReminderActivity extends Activity {


	private EditText remindText;
	private EditText scope;
	private SharedPreferences prefs;
	private Double x;
	private Double y;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newreminder);

		remindText = (EditText) findViewById(R.id.remindText);
		scope = (EditText) findViewById(R.id.scope);
		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		
		Button btnNextScreen = (Button) findViewById(R.id.btnRemind);

		// Listening to btnNextScreen event
		btnNextScreen.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				saveData();
				
				// Returns main activity after saving new reminder
				Intent nextScreen = new Intent(getApplicationContext(),
						ReminderActivity.class);

				startActivity(nextScreen);

			}
		});

	}
	
	/**
	 * save inputs as key-value.
	 * key is generated by UUID, unique
	 */

	public void saveData() {
		//get location info x? y?
		SharedPreferences.Editor editor = prefs.edit();
		// generate unique id for each reminder
		UUID uuid = UUID.randomUUID();
		String key = String.valueOf(uuid.toString());
		//saving sharedPreferences
		editor.putString(key, generateReminderItemValue(key));
		editor.commit();
	}

	/**
	 * Generates user data. Seperator is ";" what to remind ; available scope ;
	 * location info
	 * 
	 * @return
	 */

	public String generateReminderItemValue(String generatedKey) {
		StringBuilder builder = new StringBuilder();
		builder.append(generatedKey.toString());
		builder.append(Utility.SEPARATOR);
		builder.append(remindText.getText().toString());
		builder.append(Utility.SEPARATOR);
		builder.append(scope.getText().toString());
		builder.append(Utility.SEPARATOR);
		Location currentLocation = Utility.getKnownLocation(this);
		builder.append(currentLocation.getLatitude());
		builder.append(Utility.SEPARATOR);
		builder.append(currentLocation.getLongitude());
		return builder.toString();
	}
}
